package com.itheima.test.TCP.threehandshake;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Service {//服务器接受信息并返回一句话

    public static void main(String[] args) throws IOException {
        //创建服务器的socket
        ServerSocket serverSocket = new ServerSocket(1000);
        Socket socket = serverSocket.accept();
        //读数据,字节流->转换流->字符缓冲流
        InputStream is = socket.getInputStream();//字节流
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");//转换流
        BufferedReader br = new BufferedReader(isr);//字符缓冲流
        String s;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
        //返回数据
        OutputStream out = socket.getOutputStream();
        out.write("我也是黑马程序员".getBytes());
        //释放资源
        is.close();
        isr.close();
        out.close();
        br.close();
        socket.close();
        serverSocket.close();
    }
}
